<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\OrderComment;
use App\Models\Service;
use App\Services\NotifyService;
use Dingo\Api\Http\Request;

class OrderCommentController extends BaseController
{

    public $model = OrderComment::class;


    public function index(Request $request) {
        $start_at = $request->input('start_at');
        $end_at = $request->input('end_at');
        $appoint_no = $request->input('appoint_no');
        $title = $request->input('title');
        $is_good = $request->input('is_good');
        $list = OrderComment::withCertain('user', ['nickname', 'avatar_url'])
            ->when($is_good != -1, function ($q) use($is_good){
                if ($is_good)
                    $q->where('star', '>=', 3);
                else
                    $q->where('star', '<', 3);
            })
            ->when($appoint_no, function ($q) use($appoint_no) {
                $q->whereHas('appoint', function ($qt)  use($appoint_no) {
                    $qt->where('appoint_no', 'like', '%' . $appoint_no . '%');
                });
            })
            ->when($title, function ($q) use($title) {
                $q->whereHas('service', function ($qt)  use($title) {
                    $qt->where('title', 'like', '%' . $title . '%');
                });
            })
            ->startAt($start_at)->endAt($end_at)
            ->withCertain('service', ['title'])
            ->withCertain('user', ['nickname'])
            ->withCertain('appoint', ['appoint_no'])
            ->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));

        $info = null;
        return $this->paginate($list, $info);
    }


    /**
     * 删除
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Throwable
     */
    public function destroy(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required'
        ]);
        $ids = $request->input('ids');
        $arr_id = explode(",", $ids);
        return \DB::transaction(function () use ($arr_id) {
            foreach ($arr_id as $id) {
                $comment = OrderComment::find($id);
                if (!$comment)
                    continue;

                $entity_model = new Service();
                $entity_model->where('id', $comment->entity_id)->increment('comment_num', -$comment->reply_count);
                $comment->delete();
            }
            return $this->success([]);
        });
    }

    /**
     * 评论
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Throwable
     */
    public function store(Request $request) {
        $this->validate($request, [
            'id' => 'required',
            'reply_message' => 'required'
        ]);
        $user = $this->getUser();
        $input = $request->input();

        try {
            $id = \DB::transaction(function () use($input, $user){
                $comment = OrderComment::find($input['id']);
                $comment->reply_message = $input['reply_message'];
                $comment->save();

                $entity_model = new Service();
                $entity_result = $entity_model->find($comment->entity_id);
                if (!$entity_result)
                    throw new \Exception("ID对应数据不存在");

                return $input['id'];
            });
            return $this->success($id);
        } catch (\Exception $e) {
            \Log::error($e->getLine());
            return $this->failed($e->getMessage());
        }

    }

}

